# Foreign Aid and State Legitimacy in Africa: Cross-National and Sub-National 
# Evidence from Surveys, Survey Experiments, and Behavioral Games

# Table 3 and tables A15 - A15: Cross-Country Regression Results

# Parameters -------------------------------------------------------------------
# Define independent variables
IVs_without_acled <- c(" + age + muslim + urban + male + distance_capital + in_leader_adm1 + factor(afro.round) ")
IVs_china_usaid <- paste0(IVs_without_acled, " + riotsprotests_china_usaid_before1stplanned")
IVs_china <- paste0(IVs_without_acled, " + riotsprotests_china_before1stplanned")

# Load Data --------------------------------------------------------------------
data <- readRDS(file.path(data_file_path, "afro_china_data.Rds"))

# Data Subsamples --------------------------------------------------------------
# Define subsamples that remove observations near a project where the year cannot 
# be determined and near active projects (projects being implemented at time of survey)

# Dataset for all countries in sample
data_all_countries <- data %>%
  filter(noyeardata_near_china.pl10.bin == 0,
         implementing_near_china.pl10.bin == 0)

# Dataset for US aid countries
data_usaid_countries <- data %>%
  filter(iso %in% iso_usaid,
         noyeardata_near_usaid.bin == 0,
         implementing_near_usaid.bin == 0)

# Further subset US aid countries dataset by when year cannot be determined and 
# when near active projects for Chinese projects. Do for different planned
# cut-off years
data_chus_pl10 <- data_usaid_countries %>%
  filter(noyeardata_near_china.pl10.bin == 0,
         implementing_near_china.pl10.bin == 0)

data_chus_pl09 <- data_usaid_countries %>%
  filter(noyeardata_near_china.pl09.bin == 0,
         implementing_near_china.pl09.bin == 0)

data_chus_pl08 <- data_usaid_countries %>%
  filter(noyeardata_near_china.pl08.bin == 0,
         implementing_near_china.pl08.bin == 0)

# For US/China aata with planned cut-ofF in 2020, remove if near both completed 
# and planned
data_chus_pl10_rm_both_cp <- data_chus_pl10 %>%
  filter(!((completed_near_china.pl10.bin %in% 1) & (planned_near_china.pl10.bin %in% 1))) %>%
  filter(!((completed_near_usaid.bin %in% 1) & (planned_near_usaid.bin %in% 1)))
  
# Table 3 ----------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl10.bin", "planned_near_china.pl10.bin",
              "completed_near_usaid.bin",      "planned_near_usaid.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    file = file.path(tables_file_path, "table_3.tex"))

# Table A.15 -------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl09.bin", "planned_near_china.pl09.bin",
              "completed_near_usaid.bin",      "planned_near_usaid.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl09) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl09) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl09) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl09) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl09) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    file = file.path(tables_file_path, "table_a15.tex"))

# Table A.16 -------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl08.bin", "planned_near_china.pl08.bin",
              "completed_near_usaid.bin",      "planned_near_usaid.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl08) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl08) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl08) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl08) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl08) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    file = file.path(tables_file_path, "table_a16.tex"))

# Table A.17 -------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl10.count", "planned_near_china.pl10.count",
              "completed_near_usaid.count",      "planned_near_usaid.count")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    file = file.path(tables_file_path, "table_a17.tex"))

# Table A.18 ----------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl10.bin", "planned_near_china.pl10.bin",
              "completed_near_usaid.bin",      "planned_near_usaid.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10_rm_both_cp) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10_rm_both_cp) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10_rm_both_cp) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10_rm_both_cp) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china_usaid, " | iso | 0 | townvill")), data=data_chus_pl10_rm_both_cp) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    file = file.path(tables_file_path, "table_a18.tex"))

# Table A.19 -------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl10.bin", "planned_near_china.pl10.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 

make_table_2aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    covariate.labels = c("China Completed","China Planned"),
                    file = file.path(tables_file_path, "table_a19.tex"))

# Table A.20 -------------------------------------------------------------------
aid_vars <- c("completed_near_china.pl10.infra.bin",    "planned_near_china.pl10.infra.bin",
              "completed_near_china.pl10.noninfra.bin", "planned_near_china.pl10.noninfra.bin")
aid_vars_form <- aid_vars %>% paste(collapse = " + ")

trust_index_lm     <- felm(as.formula(paste("trust_index ~ ",    aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
democ_high_qual_lm <- felm(as.formula(paste("democ_high_qual ~", aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
refuse_pay_tax_lm  <- felm(as.formula(paste("refuse_pay_tax ~",  aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
gov_right_tax_lm   <- felm(as.formula(paste("gov_right_tax ~",   aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 
easy_avoid_tax_lm  <- felm(as.formula(paste("easy_avoid_tax ~",  aid_vars_form, IVs_china, " | iso | 0 | townvill")), data=data_all_countries) 

make_table_4aidvars(trust_index_lm,
                    democ_high_qual_lm,
                    refuse_pay_tax_lm,
                    gov_right_tax_lm,
                    easy_avoid_tax_lm,
                    keep = aid_vars,
                    covariate.labels = c("Infra Completed","Infra Planned","Non Infra Completed","Non Infra Planned"),
                    pvale_title_1 = "Infrastructure",
                    pvale_title_2 = "Non-Infrastructure",
                    file = file.path(tables_file_path, "table_a20.tex"))


